<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入流程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--      <el-table-column label="主键ID" align="center" prop="id"/>-->
      <el-table-column label="业务主题" align="center" prop="title"/>
      <el-table-column label="流程名称" align="center" prop="modelName"/>
      <el-table-column label="流程版本" align="center">
        <template slot-scope="scope" v-if="scope.row.modelVersion">
          <el-tag size="medium">v{{ scope.row.modelVersion }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务名称" align="center" prop="taskName"/>
      <el-table-column label="发起人" align="center" prop="initiator"/>
      <el-table-column label="发起时间" align="center" prop="insCreateTime"/>
      <el-table-column label="任务创建时间" align="center" prop="taskCreateTime"/>
      <el-table-column label="到期时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.due, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTask(scope.row)"
          >处理
          </el-button>
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['system:task:remove']"
                    >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 任务处理页面 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :rules="rules">
      <el-tabs v-model="activeName" @tab-click="handleClick" tab-position="left">
        <!--流程发起人详细信息-->
        <el-tab-pane label="发起人" name="first">
          <div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user"/>
                用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone"/>
                手机号码
                <div class="pull-right">{{ user.phonenumber }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree"/>
                所属部门
                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }}</div>
              </li>

            </ul>
          </div>
        </el-tab-pane>
        <el-tab-pane label="表单信息" name="second">
          <el-col :span="16" :offset="4">
            <div class="test-form">
              <!--              <parser :key="new Date().getTime()" :form-conf="variablesData"/>-->
              <parser :key="new Date().getTime()" :form-conf="variablesData" @submit="submitForm" ref="parser"/>
            </div>
          </el-col>
        </el-tab-pane>
        <el-tab-pane label="流程图" name="third">
          <div>
            <flow-view :flowData="flowData"/>
          </div>
        </el-tab-pane>
        <!--流程流转记录-->
        <el-tab-pane label="流转记录" name="fourth">
          <el-col :span="16" :offset="4">
            <div class="block">
              <el-timeline>
                <el-timeline-item
                  v-for="(item,index ) in flowRecordList"
                  :key="index">
                  <!--                  :icon="setIcon(item.finishTime)"
                                    :color="setColor(item.finishTime)"-->

                  <p style="font-weight: 700">{{ item.taskName }}</p>
                  <el-card :body-style="{ padding: '10px' }">
                    <el-descriptions class="margin-top" :column="1" size="small" border>
                      <el-descriptions-item v-if="item.handlerUserName" label-class-name="my-label">
                        <template slot="label"><i class="el-icon-user"></i>办理人</template>
                        {{ item.handlerUserName }}
                        <el-tag type="info" size="mini">{{ item.deptName }}</el-tag>
                      </el-descriptions-item>

                      <el-descriptions-item label-class-name="my-label">
                        <template slot="label"><i class="el-icon-date"></i>处理动作</template>
                        {{ item.handlerAction }}
                      </el-descriptions-item>

                      <el-descriptions-item v-if="item.candidate" label-class-name="my-label">
                        <template slot="label"><i class="el-icon-user"></i>候选办理</template>
                        {{ item.candidate }}
                      </el-descriptions-item>

                      <el-descriptions-item label-class-name="my-label">
                        <template slot="label"><i class="el-icon-date"></i>接收时间</template>
                        {{ item.createTime }}
                      </el-descriptions-item>

                      <el-descriptions-item v-if="item.finishTime" label-class-name="my-label">
                        <template slot="label"><i class="el-icon-date"></i>处理时间</template>
                        {{ item.createTime }}
                      </el-descriptions-item>

                      <el-descriptions-item v-if="item.duration" label-class-name="my-label">
                        <template slot="label"><i class="el-icon-time"></i>耗时</template>
                        {{ item.duration }}
                      </el-descriptions-item>

                      <el-descriptions-item v-if="item.comment" label-class-name="my-label">
                        <template slot="label"><i class="el-icon-tickets"></i>处理意见</template>
                        {{ item.comment }}
                      </el-descriptions-item>
                    </el-descriptions>
                  </el-card>
                </el-timeline-item>
              </el-timeline>
            </div>
          </el-col>
        </el-tab-pane>
        <!--        <el-tab-pane label="流转记录" name="fourth">



                </el-tab-pane>-->
        <el-tab-pane label="处理" name="five" >
          <div style="margin-left:15%;margin-bottom: 20px;font-size: 14px;">
            意见：
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="textarea">
            </el-input>

          </div>
          <el-button icon="el-icon-success" type="success" size="mini" style="margin-left: 160px"
                     @click="handleAgree()">同意
          </el-button>
          <el-button icon="el-icon-star-off" type="primary" size="mini" @click="transferClick(1)">转办</el-button>
          <el-button icon="el-icon-user" type="primary" size="mini" @click="transferClick(2)">委派</el-button>
          <el-button icon="el-icon-error" type="primary" size="mini" @click="handleRefuse">拒绝</el-button>
        </el-tab-pane>


        <!--                <el-button  icon="el-icon-edit-outline" type="primary" size="mini" @click="handleAssign">转办</el-button>-->
        <!--                <el-button  icon="el-icon-edit-outline" type="primary" size="mini" @click="handleDelegate">签收</el-button>-->
        <!--              <el-button icon="el-icon-refresh-left" type="warning" size="mini" @click="handleReturn">退回</el-button>-->
        <!--              <el-button icon="el-icon-circle-close" type="danger" size="mini" @click="handleReject">驳回</el-button>-->

      </el-tabs>
      <!--      <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>-->
    </el-dialog>

    <!--选择人员-->
    <el-dialog
      title="选择人员"
      :visible.sync="userVisible"
      width="60%"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <flow-user :checkType="checkType" :selectValues="selectValues" @handleUserSelect="handleUserSelect"></flow-user>
      <span slot="footer" class="dialog-footer">
      <el-button @click="userVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleUserSelected">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTaskMyTodo,
  getTask,
  delTask,
  delegateTask,
  listRecord,
  refuseTask,
  transferTask,
  agree,
  addTask,
  updateTask,
} from "@/api/process/task";
import {
  getFormByInsId,
  getDynamicModelPic
} from "@/api/process/ins";
import {getForm, addDeployForm, listForm} from "@/api/process/form";
import Parser from '@/components/parser/Parser'
import {getUser} from "@/api/system/user";
import FlowView from './flowview'
import FlowUser from '@/components/flow/User'

export default {
  name: "Task",
  computed: {},
  components: {Parser, FlowView, FlowUser,},
  data() {
    return {
      // 选类型
      checkType: 'single',
      // 数据回显
      selectValues: null,
      // 用户列表
      userList: this.users,
      textarea: '',
      // 模型xml数据
      xmlData: "",
      flowData: {},
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      variablesData: {}, // 流程变量数据
      flowRecordList: [], // 流程流转数据
      activeName: 'first',
      userVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processInstanceId: null,
        rev: null,
        executionId: null,
        procInstId: null,
        procDefId: null,
        caseExecutionId: null,
        caseInstId: null,
        caseDefId: null,
        modelName: null,
        parentTaskId: null,
        description: null,
        taskDefKey: null,
        owner: null,
        assignee: null,
        delegation: null,
        priority: null,
        createTime: null,
        lastUpdated: null,
        dueDate: null,
        followUpDate: null,
        suspensionState: null,
        tenantId: null,
        processDefinitionId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        textarea: [
          {required: true, message: "审批意见不能为空", trigger: "blur"}
        ],
      },
      handleType: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    transferClick(type) {
      this.handleType = type;
      this.userVisible = true;
      this.checkType = "single";
    },
    handleRefuse() {
      let data = {};
      data.comment = this.textarea;
      data.taskId = this.id
      refuseTask(data).then(response => {
        this.$modal.msgSuccess("处理成功");
        this.open = false;
        this.getList();
      });
    },
    // 选择用户后处理
    handleUserSelected() {
      this.userVisible = false;
      this.open = false;
      let data = {};
      data.comment = this.textarea;
      data.taskId = this.id
      if (this.handleType === 1) {
        // 转办
        data.transferUserId = this.selectedUserId
        transferTask(data).then(response => {
          this.$modal.msgSuccess("处理成功");
          this.open = false;
          this.getList();
        });
      } else if (this.handleType === 2) {
        // 委派
        data.delegateUserId = this.selectedUserId
        delegateTask(data).then(response => {
          this.$modal.msgSuccess("处理成功");
          this.open = false;
          this.getList();
        });
      }

    },
    handleAgree() {
      let data = {};
      data.comment = this.textarea;
      data.taskId = this.id
      agree(data).then(response => {
        this.$modal.msgSuccess("处理成功");
        this.open = false;
        this.getList();
      });
    },
    // 用户选中数据
    handleUserSelect(selection) {
      this.selectedUserId = selection.userId;
    },
    handleClick(tab, event) {
      let selected = tab.name; // 选中的
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listTaskMyTodo(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rev: null,
        executionId: null,
        procInstId: null,
        procDefId: null,
        caseExecutionId: null,
        caseInstId: null,
        caseDefId: null,
        name: null,
        parentTaskId: null,
        description: null,
        taskDefKey: null,
        owner: null,
        assignee: null,
        delegation: null,
        priority: null,
        createTime: null,
        lastUpdated: null,
        dueDate: null,
        followUpDate: null,
        suspensionState: null,
        insId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 处理任务操作 */
    handleTask(row) {
      console.log(row)
      this.id = row.id;
      // 一次性查询所有相关信息
      this.reset();
      // 设置为发起人，并查询发起人信息
      this.activeName = 'first'
      // 1. 发起人
      getUser(1).then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
      // 2. 根据任务ID查询启动表单+变量（之前都作为了流程变量存入了流程表中）
      // 表单
      getFormByInsId(row.insId).then(response => {
        this.variablesData = response.data.variables;
      });
      // 3. 动态流程图
      getDynamicModelPic(row.insId).then(res => {
        this.flowData = res.data;
      });
      // 4. 流转记录
      let query = {};
      query.insId = row.insId
      listRecord(query).then(res => {
        this.flowRecordList = res.data;
      });
      // 打开页面
      this.open = true;
      this.title = "任务处理";
/*      // 任务ID查询表单及表单提交数据
      getForm(3179).then(response => {

      });*/
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style>
.aaa {
  margin-top: 20px;
  margin-right: 100px;
}
</style>
